package com.example.demo.webmagic;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class AlibabaPageDriver {
    public static void main(String[] args){
        List<String[]> resultData = new ArrayList<>();

        int page = 90;

        /*while (true){
            boolean res = getPage(page,resultData);

            if(!res){
                break;
            }

            page ++;
        }

        createExcel(resultData);*/

        mergeExcel();

    }

    public static boolean getPage(int page,List<String[]> resultData){
        WebDriver driver = new ChromeDriver();

        try{
            //String url = "https://www.alibaba.com/products/wheel_alignment.html?IndexArea=product_en&page="+page;
            String url = "https://www.alibaba.com/products/coupling.html?IndexArea=product_en&page="+page;
            System.out.println("开始抓取页面："+url);

            driver.get(url);

            //等待元素加载出来
            /*boolean isLoad = waitElementLoaded(driver);
            if(!isLoad){
                System.out.println("页面全部抓取完成！");
                return isLoad;
            }
*/
            Thread.sleep(2000);
            //向下滚动
            for(int i=0;i<10;i++){
                ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,1000)");
                Thread.sleep(500);
            }

            String xp1 = "//*[@id=\"root\"]/div/div[3]/div[2]/div/div/div/*";
            String xp2 = "//div[@class='list-no-v2-outter J-offer-wrapper']";
            List<WebElement> elements = driver.findElements(By.xpath(xp2));

            System.out.println("抓取到商品数量："+elements == null?0:elements.size());

            if(elements == null || elements.size() <= 0){
                return false;
            }

            if(elements != null && elements.size() > 0){
                for(int i=0;i<elements.size();i++){
                    WebElement element = elements.get(i);

                    String title = getElement(".elements-title-normal__content",element);
                    String price = getElement(".elements-offer-price-normal",element);
                    String country = getElement(".seller-tag__country",element);
                    String comoany = getElement(".list-no-v2-decisionsup__element",element);
                    String minOrd = getElement(".element-offer-minorder-normal",element);

                    /*System.out.println("_________________________");
                    System.out.println("title"+(i+1)+"："+title);
                    System.out.println("price"+(i+1)+"："+price);
                    System.out.println("country"+(i+1)+"："+country);
                    System.out.println("comoany"+(i+1)+"："+comoany);
                    System.out.println("minOrd"+(i+1)+"："+minOrd);*/

                    String[] data = new String[]{title,price,country,comoany,minOrd};
                    resultData.add(data);
                }
            }


            driver.quit();

            return true;

        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            driver.quit();
        }

        return false;
    }

    public static String getElement(String css,WebElement element){
        try{
            String text = element.findElement(By.cssSelector(css)).getText();
            return text;

        }catch (Exception e){

        }

        return "";
    }

    public static boolean waitElementLoaded(WebDriver driver){
        try{
            long timeOutInSeconds = 20;
            WebDriverWait webDriverWait = new WebDriverWait(driver,timeOutInSeconds);
            webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#root > div > div.seb-pagination > div > div.seb-pagination__pages > a:nth-child(10)")));

            return true;
        }catch (Exception e){

        }

        return false;
    }

    public static void createExcel(List<String[]> resultData){
        try{
            HSSFWorkbook workbook = new HSSFWorkbook();

            HSSFSheet sheet = workbook.createSheet();

            int rowIndex = 0;
            int cellIndex = 0;

            String[] header = new String[]{"标题","价格","国家","公司","最低起订量"};
            HSSFRow row = sheet.createRow(rowIndex);

            for(String h : header){
                HSSFCell cell = row.createCell(cellIndex);
                cellIndex ++;
                cell.setCellValue(h);
            }

            for(int i=0;i<resultData.size();i++){
                String[] data = resultData.get(i);

                rowIndex ++;
                cellIndex = 0;
                row = sheet.createRow(rowIndex);

                for(String d : data){
                    HSSFCell cell = row.createCell(cellIndex);
                    cellIndex ++;
                    cell.setCellValue(d);
                }
            }

            String outPath = "D:\\coupling.xls";
            workbook.write(new File(outPath));

            workbook.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void mergeExcel(){
        try{
            String excel1 = "D:\\coupling_1_.xls";
            String excel2 = "D:\\coupling_90_.xls";
            HSSFWorkbook workbook1 = new HSSFWorkbook(new FileInputStream(excel1));
            HSSFSheet sheet1 = workbook1.getSheetAt(0);
            int maxRowNum1 = sheet1.getLastRowNum();

            HSSFWorkbook workbook2 = new HSSFWorkbook(new FileInputStream(excel2));
            HSSFSheet sheet2 = workbook2.getSheetAt(0);
            int maxRowNum2 = sheet2.getLastRowNum();

            for(int i= 1;i<=maxRowNum2;i++){
                maxRowNum1 ++;

                HSSFRow row2 = sheet2.getRow(i);
                HSSFRow row1 = sheet1.getRow(maxRowNum1)==null?sheet1.createRow(maxRowNum1):sheet1.getRow(maxRowNum1);

                for(int j=0;j<5;j++){
                    HSSFCell cell2 = row2.getCell(j);

                    HSSFCell cell1 = row1.createCell(j);
                    cell1.setCellValue(cell2.getStringCellValue());
                }


            }

            workbook1.write(new File("D:\\coupling.xls"));

            workbook1.close();
            workbook2.close();


        }catch (Exception e){

        }
    }

}
